home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter15 / isohex15_1 / isohex15_1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-01  |  10.9 KB  |  431 lines

  1. /*****************************************************************************
  2. IsoHex15_1.cpp
  3. Ernest S. Pazera
  4. 01AUG2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Requires the following libs:
  7. ddraw.lib, dxguid.lib
  8. Requires the following files:
  9. DDFuncs.h.cpp, GDICanvas.h/cpp, IsoMouseMap.h/cpp,
  10. IsoScroller.h/cpp, IsoTilePlotter.h/cpp, IsoTileWalker.h/cpp
  11. TileSet.h/cpp. IsoHexCore.h, IsoHexDefs.h
  12. *****************************************************************************/
  13.  
  14. //////////////////////////////////////////////////////////////////////////////
  15. //INCLUDES
  16. //////////////////////////////////////////////////////////////////////////////
  17. #define WIN32_LEAN_AND_MEAN  
  18.  
  19. #include <windows.h>  
  20. #include "DDFuncs.h"
  21. #include "TileSet.h"
  22. #include "IsoHexCore.h"
  23.  
  24.  
  25. //////////////////////////////////////////////////////////////////////////////
  26. //DEFINES
  27. //////////////////////////////////////////////////////////////////////////////
  28. //name for our window class
  29. #define WINDOWCLASS "ISOHEX15"
  30. //title of the application
  31. #define WINDOWTITLE "IsoHex 15-1"
  32.  
  33. const int MAPWIDTH=20;
  34. const int MAPHEIGHT=20;
  35.  
  36. //////////////////////////////////////////////////////////////////////////////
  37. //PROTOTYPES
  38. //////////////////////////////////////////////////////////////////////////////
  39. bool Prog_Init();//game data initalizer
  40. void Prog_Loop();//main game loop
  41. void Prog_Done();//game clean up
  42.  
  43. //////////////////////////////////////////////////////////////////////////////
  44. //GLOBALS
  45. //////////////////////////////////////////////////////////////////////////////
  46. HINSTANCE hInstMain=NULL;//main application handle
  47. HWND hWndMain=NULL;//handle to our main window
  48.  
  49. //directdraw
  50. LPDIRECTDRAW7 lpdd=NULL;
  51. LPDIRECTDRAWSURFACE7 lpddsMain=NULL;
  52. LPDIRECTDRAWSURFACE7 lpddsBack=NULL;
  53. LPDIRECTDRAWCLIPPER lpddClip=NULL;
  54.  
  55. //tilesets
  56. CTileSet tsIso;//main tileset
  57. CTileSet tsCursor;//cursor
  58.  
  59. //isohexcore components
  60. CTilePlotter TilePlotter;//plotter
  61. CTileWalker TileWalker;//walker
  62. CScroller Scroller;//scroller
  63. CMouseMap MouseMap;//mousemap
  64.  
  65. POINT ptCursor;//keep track of the cursor
  66. POINT ptScroll;//keep track of how quickly we scroll
  67.  
  68. int iMap[MAPWIDTH][MAPHEIGHT];//map array
  69.  
  70. //////////////////////////////////////////////////////////////////////////////
  71. //WINDOWPROC
  72. //////////////////////////////////////////////////////////////////////////////
  73. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  74. {
  75.     //which message did we get?
  76.     switch(uMsg)
  77.     {
  78.     case WM_KEYDOWN:
  79.         {
  80.             switch(wParam)
  81.             {
  82.             case VK_ESCAPE:
  83.                 {
  84.                     DestroyWindow(hWndMain);
  85.                 }break;
  86.             case '1':
  87.                 {
  88.                     //set up the iso engine for slide maps
  89.                     TileWalker.SetMapType(ISOMAP_SLIDE);//set walker to slide mapping
  90.  
  91.                     TilePlotter.SetMapType(ISOMAP_SLIDE);//set plotter to slide mapping
  92.  
  93.                     //recalculate the scroller
  94.                     Scroller.CalcWorldSpace(&TilePlotter,&tsIso.GetTileList()[0].rcDstExt,MAPWIDTH,MAPHEIGHT);
  95.                     Scroller.CalcAnchorSpace();
  96.  
  97.                     //set the screen anchor back to zero
  98.                     Scroller.GetAnchor()->x=0;
  99.                     Scroller.GetAnchor()->y=0;
  100.                 }break;
  101.             case '2':
  102.                 {
  103.                     //set up iso engine for staggered maps
  104.                     TileWalker.SetMapType(ISOMAP_STAGGERED);//set walker to staggered mapping
  105.  
  106.                     TilePlotter.SetMapType(ISOMAP_STAGGERED);//set plotter to staggered mapping
  107.  
  108.                     //recalculate the scroller
  109.                     Scroller.CalcWorldSpace(&TilePlotter,&tsIso.GetTileList()[0].rcDstExt,MAPWIDTH,MAPHEIGHT);
  110.                     Scroller.CalcAnchorSpace();
  111.  
  112.                     //reset the anchor to (0,0)
  113.                     Scroller.GetAnchor()->x=0;
  114.                     Scroller.GetAnchor()->y=0;
  115.                 }break;
  116.             case '3':
  117.                 {
  118.                     //set up iso engine for diamond maps
  119.                     TileWalker.SetMapType(ISOMAP_DIAMOND);//set walker to diamond map mode
  120.  
  121.                     TilePlotter.SetMapType(ISOMAP_DIAMOND);//set plotter to diamond mapping
  122.  
  123.                     //recalculate the scroller spaces
  124.                     Scroller.CalcWorldSpace(&TilePlotter,&tsIso.GetTileList()[0].rcDstExt,MAPWIDTH,MAPHEIGHT);
  125.                     Scroller.CalcAnchorSpace();
  126.  
  127.                     //reset the anchor to (0,0)
  128.                     Scroller.GetAnchor()->x=0;
  129.                     Scroller.GetAnchor()->y=0;
  130.                 }break;
  131.             }
  132.         }break;
  133.     case WM_MOUSEMOVE:
  134.         {
  135.             //grab mouse x and y
  136.             int x=LOWORD(lParam);
  137.             int y=HIWORD(lParam);
  138.  
  139.             //reset scrolling speeds to zero
  140.             ptScroll.x=0;
  141.             ptScroll.y=0;
  142.  
  143.             //left scroll?
  144.             if(x<8) ptScroll.x=x-8;
  145.  
  146.             //upward scroll?
  147.             if(y<8) ptScroll.y=y-8;
  148.  
  149.             //right scroll?
  150.             if(x>=632) ptScroll.x=x-632;
  151.  
  152.             //downward scroll?
  153.             if(y>=472) ptScroll.y=y-472;
  154.         }break;
  155.     case WM_DESTROY://the window is being destroyed
  156.         {
  157.  
  158.             //tell the application we are quitting
  159.             PostQuitMessage(0);
  160.  
  161.             //handled message, so return 0
  162.             return(0);
  163.  
  164.         }break;
  165.     case WM_PAINT://the window needs repainting
  166.         {
  167.             //a variable needed for painting information
  168.             PAINTSTRUCT ps;
  169.             
  170.             //start painting
  171.             HDC hdc=BeginPaint(hwnd,&ps);
  172.  
  173.             /////////////////////////////
  174.             //painting code would go here
  175.             /////////////////////////////
  176.  
  177.             //end painting
  178.             EndPaint(hwnd,&ps);
  179.                         
  180.             //handled message, so return 0
  181.             return(0);
  182.         }break;
  183.     }
  184.  
  185.     //pass along any other message to default message handler
  186.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  187. }
  188.  
  189.  
  190. //////////////////////////////////////////////////////////////////////////////
  191. //WINMAIN
  192. //////////////////////////////////////////////////////////////////////////////
  193. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  194. {
  195.     //assign instance to global variable
  196.     hInstMain=hInstance;
  197.  
  198.     //create window class
  199.     WNDCLASSEX wcx;
  200.  
  201.     //set the size of the structure
  202.     wcx.cbSize=sizeof(WNDCLASSEX);
  203.  
  204.     //class style
  205.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  206.  
  207.     //window procedure
  208.     wcx.lpfnWndProc=TheWindowProc;
  209.  
  210.     //class extra
  211.     wcx.cbClsExtra=0;
  212.  
  213.     //window extra
  214.     wcx.cbWndExtra=0;
  215.  
  216.     //application handle
  217.     wcx.hInstance=hInstMain;
  218.  
  219.     //icon
  220.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  221.  
  222.     //cursor
  223.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  224.  
  225.     //background color
  226.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  227.  
  228.     //menu
  229.     wcx.lpszMenuName=NULL;
  230.  
  231.     //class name
  232.     wcx.lpszClassName=WINDOWCLASS;
  233.  
  234.     //small icon
  235.     wcx.hIconSm=NULL;
  236.  
  237.     //register the window class, return 0 if not successful
  238.     if(!RegisterClassEx(&wcx)) return(0);
  239.  
  240.     //create main window
  241.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_POPUP | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  242.  
  243.     //error check
  244.     if(!hWndMain) return(0);
  245.  
  246.     //if program initialization failed, then return with 0
  247.     if(!Prog_Init()) return(0);
  248.  
  249.     //message structure
  250.     MSG msg;
  251.  
  252.     //message pump
  253.     for(;;)    
  254.     {
  255.         //look for a message
  256.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  257.         {
  258.             //there is a message
  259.  
  260.             //check that we arent quitting
  261.             if(msg.message==WM_QUIT) break;
  262.             
  263.             //translate message
  264.             TranslateMessage(&msg);
  265.  
  266.             //dispatch message
  267.             DispatchMessage(&msg);
  268.         }
  269.  
  270.         //run main game loop
  271.         Prog_Loop();
  272.     }
  273.     
  274.     //clean up program data
  275.     Prog_Done();
  276.  
  277.     //return the wparam from the WM_QUIT message
  278.     return(msg.wParam);
  279. }
  280.  
  281. //////////////////////////////////////////////////////////////////////////////
  282. //INITIALIZATION
  283. //////////////////////////////////////////////////////////////////////////////
  284. bool Prog_Init()
  285. {
  286.     //create IDirectDraw object
  287.     lpdd=LPDD_Create(hWndMain,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT);
  288.  
  289.     //set display mode
  290.     lpdd->SetDisplayMode(640,480,16,0,0);
  291.  
  292.     //create primary surface
  293.     lpddsMain=LPDDS_CreatePrimary(lpdd,1);
  294.  
  295.     //get back buffer
  296.     lpddsBack=LPDDS_GetSecondary(lpddsMain);
  297.  
  298.     //create clipper
  299.     lpdd->CreateClipper(0,&lpddClip,NULL);
  300.  
  301.     //associate window with the clipper
  302.     lpddClip->SetHWnd(0,hWndMain);
  303.  
  304.     //attach clipper to back buffer
  305.     lpddsBack->SetClipper(lpddClip);
  306.  
  307.     //load in the mousemap
  308.     MouseMap.Load("MouseMap.bmp");
  309.  
  310.     //set up the tile plotter
  311.     TilePlotter.SetMapType(ISOMAP_DIAMOND);//diamond mode
  312.     TilePlotter.SetTileSize(MouseMap.GetWidth(),MouseMap.GetHeight());//grab width and height from mousemap
  313.  
  314.     //set up tile walker to diamond mode
  315.     TileWalker.SetMapType(ISOMAP_DIAMOND);
  316.  
  317.     //set up screeen space
  318.     RECT rcTemp;
  319.     SetRect(&rcTemp,0,0,640,480);
  320.     Scroller.SetScreenSpace(&rcTemp);
  321.  
  322.     //load in tiles and cursor
  323.     tsIso.Load(lpdd,"Tiles.bmp");
  324.     tsCursor.Load(lpdd,"cursor.bmp");
  325.  
  326.     //grab tile extent from tileset
  327.     CopyRect(&rcTemp,&tsIso.GetTileList()[0].rcDstExt);
  328.  
  329.     //calculate the worldspace
  330.     Scroller.CalcWorldSpace(&TilePlotter,&rcTemp,MAPWIDTH,MAPHEIGHT);
  331.  
  332.     //calculate the mousemap reference point
  333.     MouseMap.CalcReferencePoint(&TilePlotter,&rcTemp);
  334.  
  335.     //calculate anchor space
  336.     Scroller.CalcAnchorSpace();
  337.  
  338.     //set wrap modes for scroller
  339.     Scroller.SetHWrapMode(WRAPMODE_CLIP);
  340.     Scroller.SetVWrapMode(WRAPMODE_CLIP);
  341.  
  342.     //set scroller anchor to (0,0)
  343.     Scroller.GetAnchor()->x=0;
  344.     Scroller.GetAnchor()->y=0;
  345.  
  346.     //attach scrolelr and tilewalker to mousemap
  347.     MouseMap.SetScroller(&Scroller);
  348.     MouseMap.SetTileWalker(&TileWalker);
  349.  
  350.     //set up the map to a random tilefield
  351.     for(int x=0;x<MAPWIDTH;x++)
  352.     {
  353.         for(int y=0;y<MAPHEIGHT;y++)
  354.         {
  355.             iMap[x][y]=rand()%tsIso.GetTileCount();
  356.         }
  357.     }
  358.  
  359.     return(true);//return success
  360. }
  361.  
  362. //////////////////////////////////////////////////////////////////////////////
  363. //CLEANUP
  364. //////////////////////////////////////////////////////////////////////////////
  365. void Prog_Done()
  366. {
  367.     //release main/back surfaces
  368.     LPDDS_Release(&lpddsMain);
  369.  
  370.     //release clipper
  371.     LPDDCLIP_Release(&lpddClip);
  372.  
  373.     //release directdraw
  374.     LPDD_Release(&lpdd);
  375. }
  376.  
  377. //////////////////////////////////////////////////////////////////////////////
  378. //MAIN GAME LOOP
  379. //////////////////////////////////////////////////////////////////////////////
  380. void Prog_Loop()
  381. {
  382.     //clear out backbuffer
  383.     DDBLTFX ddbltfx;
  384.     DDBLTFX_ColorFill(&ddbltfx,0);
  385.     lpddsBack->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);
  386.     
  387.     //move the anchor based on scrolling speed
  388.     Scroller.MoveAnchor(ptScroll.x,ptScroll.y);
  389.  
  390.     //plot our tiles
  391.     POINT ptPlot;
  392.     POINT ptMap;
  393.     for(ptMap.y=0;ptMap.y<MAPHEIGHT;ptMap.y++)
  394.     {
  395.         for(ptMap.x=0;ptMap.x<MAPWIDTH;ptMap.x++)
  396.         {
  397.             //plot the tile
  398.             ptPlot=TilePlotter.PlotTile(ptMap);
  399.  
  400.             //convert from world to screen
  401.             ptPlot=Scroller.WorldToScreen(ptPlot);
  402.  
  403.             //put the tile
  404.             tsIso.PutTile(lpddsBack,ptPlot.x,ptPlot.y,iMap[ptMap.x][ptMap.y]);
  405.         }
  406.     }
  407.  
  408.     //grab the mouse position
  409.     POINT ptMouse;
  410.     GetCursorPos(&ptMouse);
  411.     //map the mouse
  412.     ptCursor=MouseMap.MapMouse(ptMouse);
  413.  
  414.     //clip the cursor to valid map coordinates
  415.     if(ptCursor.x<0) ptCursor.x=0;
  416.     if(ptCursor.y<0) ptCursor.y=0;
  417.     if(ptCursor.x>(MAPWIDTH-1)) ptCursor.x=MAPWIDTH-1;
  418.     if(ptCursor.y>(MAPHEIGHT-1)) ptCursor.y=MAPHEIGHT-1;
  419.  
  420.     //plot the cursor
  421.     ptPlot=TilePlotter.PlotTile(ptCursor);
  422.     //convert world to screen
  423.     ptPlot=Scroller.WorldToScreen(ptPlot);
  424.     //put the cursor on screen
  425.     tsCursor.PutTile(lpddsBack,ptPlot.x,ptPlot.y,0);
  426.  
  427.     //flip to show the back buffer
  428.     lpddsMain->Flip(0,DDFLIP_WAIT);
  429. }
  430.  
  431.